package cn.itsource.pethome.org.service.impl;

import cn.itsource.pethome.org.domain.Department;
import cn.itsource.pethome.org.mapper.DepartmentMapper;
import cn.itsource.pethome.org.query.DepartmentQuery;
import cn.itsource.pethome.org.service.DepartmentService;
import cn.itsource.pethome.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);

    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }

    @Override
    public Department loadById(Long id) {
        Department department = departmentMapper.loadById(id);
        return department;
    }

    @Override
    public List<Department> loadAll() {
        List<Department> departments = departmentMapper.loadAll();
        return departments;
    }


    @Override
    public PageList<Department> loadPageByQuery(DepartmentQuery query) {
        //1.查询总条数如果为0就不用分页了
        System.out.println(query);
        Long total = departmentMapper.findCountByQuery(query);
        if (total.equals(0L)){
            return new PageList<Department>();
        }
        //查询分页列表
        List<Department> result = departmentMapper.fandListByQuery(query);
            return new PageList<Department>(total,result);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        departmentMapper.batchRemove(ids);
    }

}
